#include "TROOT.h"
#include "TCanvas.h"
#include "RooFit.h"
#include "RooRealVar.h"
#include "RooDataSet.h"
#include "RooDataHist.h"
#include "RooPolynomial.h"
#include "RooPlot.h"
#include "RooKeysPdf.h"
#include "RooAddPdf.h"
#include "iostream"
#include "TApplication.h"
#include "TPaveText.h"
#include "/home/yukp/headfiles/Xic2XimPi/RooAngularPdf.h"
using namespace RooFit;
#include<ctime>
void pseudo()
{
/*
int times=REPLACE;
RooRandom::randomGenerator()->SetSeed(SEEDRAND);

*/
long SEED=1561965814651;
RooRandom::randomGenerator()->SetSeed(SEED);
double purity=0.75;//fraction of signal events and total events
int total=30000;
double efficiency=1;
int total_events=total*efficiency;//totalevent Poison distribution


//initial value
double alpha_Xic=-0.64;
double alpha_Xim=-0.401;
double PhiXic_initial=-22.5;
double PhiXim_initial=-2.1;

double Px_initial=0;
double Py_initial=0.01;
double Pz_initial=0.1;

double beta_Xic=sqrt(1-pow(alpha_Xic,2))*sin(PhiXic_initial/180*TMath::Pi());
double beta_Xim=sqrt(1-pow(alpha_Xim,2))*sin(PhiXim_initial/180*TMath::Pi());

double gamma_Xic=sqrt(1-pow(alpha_Xic,2))*cos(PhiXic_initial/180*TMath::Pi());
double gamma_Xim=sqrt(1-pow(alpha_Xim,2))*cos(PhiXim_initial/180*TMath::Pi());


RooRealVar Phi_Xic("Phi_Xic","Phi_Xic",PhiXic_initial,-180,180);

RooRealVar Phi_Xim("Phi_Xim","Phi_Xim",PhiXim_initial,-180,180);

RooRealVar alphaXic("alphaXic","alphaXic",alpha_Xic,-1,1);
RooFormulaVar betaXic("betaXic","sqrt(1-@0*@0)*sin(@1*TMath::Pi()/180)",RooArgList(alphaXic,Phi_Xic)); 
RooFormulaVar gammaXic("gammaXic","sqrt(1-@0*@0)*cos(@1*TMath::Pi()/180)",RooArgList(alphaXic,Phi_Xic)); 

RooRealVar alphaXim("alphaXim","alphaXim",alpha_Xim);//+- 0.010
RooFormulaVar betaXim("betaXim","sqrt(1-@0*@0)*sin(@1*TMath::Pi()/180)",RooArgList(alphaXim,Phi_Xim)); 
RooFormulaVar gammaXim("gammaXim","sqrt(1-@0*@0)*cos(@1*TMath::Pi()/180)",RooArgList(alphaXim,Phi_Xim)); 

RooRealVar alphaLam("alphaLam","alphaLam",0.732);

RooRealVar Px("Px","Px",Px_initial,-1,1);
RooRealVar Py("Py","Py",Py_initial,-1,1);
RooRealVar Pz("Pz","Pz",Pz_initial,-1,1);

RooRealVar theta0("theta0","theta0",0,TMath::Pi());
RooRealVar theta1("theta1","theta1",0,TMath::Pi());
RooRealVar theta2("theta2","theta2",0,TMath::Pi());
RooRealVar phi0("phi0","phi0",-1*TMath::Pi(),TMath::Pi());
RooRealVar phi1("phi1","phi1",-1*TMath::Pi(),TMath::Pi());
RooRealVar phi2("phi2","phi2",-1*TMath::Pi(),TMath::Pi());



 RooAngularPdf *sigpdf= new RooAngularPdf("sigpdf","angluar", phi0, theta0, phi1, theta1, phi2, theta2, alphaXic,  betaXic,  gammaXic, alphaXim, betaXim, gammaXim, alphaLam,  Px, Py, Pz);    
RooDataSet *AngData = sigpdf->generate(RooArgSet( phi0, theta0, phi1, theta1, phi2, theta2), total_events);
AngData->write(TString("toys/dat/refer.dat"));
// generate the reference data
for (int i=0;i<1000;i++)
{
int total_events=gRandom->Poisson(total);//totalevent Poison distribution
RooRealVar Phi_Xic("Phi_Xic","Phi_Xic",PhiXic_initial,-180,180);

RooRealVar Phi_Xim("Phi_Xim","Phi_Xim",PhiXim_initial,-180,180);

RooRealVar alphaXic("alphaXic","alphaXic",alpha_Xic,-1,1);
RooFormulaVar betaXic("betaXic","sqrt(1-@0*@0)*sin(@1*TMath::Pi()/180)",RooArgList(alphaXic,Phi_Xic)); 
RooFormulaVar gammaXic("gammaXic","sqrt(1-@0*@0)*cos(@1*TMath::Pi()/180)",RooArgList(alphaXic,Phi_Xic)); 

RooRealVar alphaXim("alphaXim","alphaXim",alpha_Xim);//+- 0.010
RooFormulaVar betaXim("betaXim","sqrt(1-@0*@0)*sin(@1*TMath::Pi()/180)",RooArgList(alphaXim,Phi_Xim)); 
RooFormulaVar gammaXim("gammaXim","sqrt(1-@0*@0)*cos(@1*TMath::Pi()/180)",RooArgList(alphaXim,Phi_Xim)); 

RooRealVar alphaLam("alphaLam","alphaLam",0.732);

RooRealVar Px("Px","Px",Px_initial,-1,1);
RooRealVar Py("Py","Py",Py_initial,-1,1);
RooRealVar Pz("Pz","Pz",Pz_initial,-1,1);

RooRealVar theta0("theta0","theta0",0,TMath::Pi());
RooRealVar theta1("theta1","theta1",0,TMath::Pi());
RooRealVar theta2("theta2","theta2",0,TMath::Pi());
RooRealVar phi0("phi0","phi0",-1*TMath::Pi(),TMath::Pi());
RooRealVar phi1("phi1","phi1",-1*TMath::Pi(),TMath::Pi());
RooRealVar phi2("phi2","phi2",-1*TMath::Pi(),TMath::Pi());



 RooAngularPdf *sigpdf= new RooAngularPdf("sigpdf","angluar", phi0, theta0, phi1, theta1, phi2, theta2, alphaXic,  betaXic,  gammaXic, alphaXim, betaXim, gammaXim, alphaLam,  Px, Py, Pz);    
RooDataSet *AngData = sigpdf->generate(RooArgSet( phi0, theta0, phi1, theta1, phi2, theta2), total_events);
AngData->write(TString("toys/dat/toy_Ang")+i+TString(".dat"));
}

}
